package cn.tedu.api;

import java.util.Objects;

/**本类用于测试顶级父类Object*/
public class TestObject {
    public static void main(String[] args) {
        Student s1=new Student("张大彪",15);
        Student s11=new Student("张大彪",15);
        Student s2=new Student();

        System.out.println(s1);//打印的是地址值
        System.out.println(s2);
        //测试hashCode()
        /**返回该对象的哈希码值，用来区分不同的对象，不同的对象生成的哈希码值不同
         * 查看源码的方式，按住Ctrl，鼠标移动到目标位置点击*/
        System.out.println(s1 == s11);//false
        System.out.println(s1.equals(s11));
        System.out.println(s11.equals(s1));
        System.out.println(s1.hashCode());
        System.out.println(s11.hashCode());
        System.out.println(s2.hashCode());
    }
}
class Student{
    String name;
    int age;

    public Student() {
    }

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
        System.out.println("这是构造方法");
    }

    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Student student = (Student) o;
        return age == student.age && Objects.equals(name, student.name);
    }

    @Override
    public int hashCode() {
        return Objects.hash(name, age);
    }
}